/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys.donto.servico;

import java.util.List;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import sys.donto.util.GenericDAO;
import sys.donto.util.HibernateUtil;

/**
 *
 * @author JOABB
 */
public class ServicoDAO extends GenericDAO<Servico>{
     public ServicoDAO() {
        super(Servico.class);
    }
    public List <Servico> pesquisaDescricao(String Descricao){
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        
        List <Servico> servicos = (List<Servico>) getSessao().createCriteria(Servico.class).
                add(Restrictions.ilike("servico", Descricao, MatchMode.ANYWHERE)).
                addOrder(Order.asc("servico")).list();
        
        getSessao().close();
        return servicos;
    }
    
    public Servico pesquisaId(int id){
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        
        Servico servico = (Servico) getSessao().createCriteria(Servico.class).
                add(Restrictions.eq("id", id)).uniqueResult();
        
        getSessao().close();
        return servico;
    }
    
    public void salvar(Servico serv){
        if(serv.getId()==0){
            adicionar(serv);
        } else {
            atualizar(serv);
        }
    }

    
}
